<?

class adsController extends iMVC_Controller{



    public function addAds(){
    

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);

        session_start();
        $user=$_SESSION['user'];
        $name=$_POST["name"];
        $address=$_POST["address"];
        $content=$_POST["content"];
        $pic=$_POST["pic"];
        if(!empty($user)){
            $ads=new adsModel(); 
            $ads->ads_name=$name;
            $ads->ads_address=$address;
            $ads->ads_content=$content;
            $ads->ads_pic=$pic;
            $ads->user_id=$user->user_id;
            $ads->ads_time=time();
            $ads->save();
            header("Content-Type:text/html;charset=utf-8");
            //print_r($this->ln->addSuc);
            echo "<script>alert('".$this->ln->addSuc."');window.location.href='".$this->getBaseUrl()."user/center';</script>";
        }
        else{
            echo "<script>alert('".$this->ln->loginFirst."');history.go(-1);</script>";
        
        }

    
    
    }
    public function addForm(){
    

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);
    

        session_start();
        $user=$_SESSION['user'];
        if(empty($user)){

            header("Location:".$this->getBaseUrl()."user/login");

        }
        $this->display("addAds.tpl");
    
    }
    public function adsList(){

        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);
 
        session_start(); 
        $page=$_GET['page']; 
        $page=empty($page)?1:$page;
        //$size=$_GET['size'];
        //$page=2;
        $size=8;
        $ads=new adsModel();
        $all=ceil(count($ads->fetchAll())/$size); 
        $this->assign("rs",$ads->fetchLimit(($page-1)*$size,$size));
        $this->assign("topRs",$ads->getTopAds());
        $this->assign("all",$all);
        $this->assign("page",$page);
        if($page>0){
            $this->assign("pre",$page-1);
        }
        if($page<$all){
        
            $this->assign("last",$page+1);
        
        }   
    
        $this->display("ads-list.tpl");    
    
    }


    public function search(){
    
        $slider=new sliderModel();
        $sRs=$slider->fetchAll();
        $this->assign("slider",$sRs);
        
        $keyword=$_REQUEST["keyword"];

        $ads=new adsModel();
        $rs=$ads->fetchByKeyWord($keyword);
        $this->assign("topRs",$ads->getTopAds());
        if($rs){
            $this->assign("rs",$rs);
        }
        else{
            $this->assign("tips","没有搜索结果"); 
        
        }
        $this->display("ads-list.tpl");   

    
    }


}
// vim600:ts=4 st=4 foldmethod=marker foldmarker=<<<,>>>
// vim600:syn=php commentstring=//%s
